package org.bcncoding.appengine.droiding.server.service;

import java.util.ArrayList;
import java.util.List;

import org.bcncoding.appengine.droiding.client.service.AdminService;
import org.bcncoding.appengine.droiding.server.dao.ServiceProviderDao;
import org.bcncoding.appengine.droiding.server.entity.ServiceProvider;
import org.bcncoding.appengine.droiding.server.pojo.ServiceProviderInfo;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class AdminServiceImpl extends RemoteServiceServlet implements
		AdminService {

	private static final long serialVersionUID = -7840875080990211816L;

	private ServiceProviderDao dao = new ServiceProviderDao(
			ServiceProvider.class);

	public List<ServiceProviderInfo> getAllServiceProvider(int startRow,
			int maxRow) {
		List<ServiceProvider> providers = dao.getAllPaged(startRow, maxRow);
		List<ServiceProviderInfo> infos = new ArrayList<ServiceProviderInfo>();
		for (ServiceProvider p : providers) {
			ServiceProviderInfo info = new ServiceProviderInfo();
			info.setId("" + p.getId().getId());
			info.setKey(p.getKey());
			info.setType(p.getType().getValue());
			info.setLatitude(p.getLatitude().toString());
			info.setLongitude(p.getLongitude().toString());
			info.setLocation(p.getLocation());
			info.setName(p.getName());
			infos.add(info);
		}
		return infos;
	}

	public Integer countServiceProvider() {
		Integer result = dao.count();
		return result;
	}

	public Boolean deleteServiceProvider(String key) {
		ServiceProvider provider = dao.getByKey(key);
		dao.delete(provider);
		//Como devolver un valor void en un método Async?
		return true;
	}

}
